package ca.etsmtl.log660.view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import ca.etsmtl.log660.controller.ControllerFacade;
import ca.etsmtl.log660.entity.Film;

public class MainFrame extends JFrame implements ActionListener {
	
	public static void main(String[] args) {
		new MainFrame();
	}
	
	private static final long serialVersionUID = 8071090840469541543L;
	private static final Dimension FRAME_SIZE = new Dimension(850, 450);
	private static final String LBL_SEARCH_MOVIE = "Rechercher";
	private static final String LBL_RENT_MOVIE = "Louer le film";
	
	private ControllerFacade controller;
	private JPanel contentPanel;
	private JPanel menuPanel;
	private JButton btSearchMovie;
	private JButton btRentMovie;
	private SearchMoviePanel searchMoviePanel;

	public MainFrame() {
		this.controller = new ControllerFacade();
		this.contentPanel = new JPanel(new BorderLayout());
		this.menuPanel = new JPanel(new GridLayout(0, 2));
		this.btSearchMovie = new JButton(LBL_SEARCH_MOVIE);
		this.btSearchMovie.addActionListener(this);
		this.btSearchMovie.setEnabled(false);
		this.btRentMovie = new JButton(LBL_RENT_MOVIE);
		this.btRentMovie.addActionListener(this);
		this.btRentMovie.setEnabled(false);
		
		this.searchMoviePanel = new SearchMoviePanel();
		
		this.menuPanel.add(this.btSearchMovie);
		this.menuPanel.add(this.btRentMovie);
		
		this.contentPanel.add(this.menuPanel, BorderLayout.PAGE_END);
		this.contentPanel.add(this.searchMoviePanel, BorderLayout.CENTER);
		
		this.setContentPane(this.contentPanel);
		
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(FRAME_SIZE);
		this.setPreferredSize(FRAME_SIZE);
		this.setVisible(true);
		this.setLocationRelativeTo(null);
		
		//Show the login frame
		new LoginFrame(this);
	}
	
	public void userLogged() {
		this.btSearchMovie.setEnabled(true);
		this.btRentMovie.setEnabled(true);
	}
	
	public void userCancelledLogin() {
		System.exit(0);
	}

	private void launchSearch() {
		List<Film> searchedFilms = this.controller.searchMovie(
				this.searchMoviePanel.getTitlesFilters(),
				this.searchMoviePanel.getStartYearFilter(),
				this.searchMoviePanel.getEndYearFilter(),
				this.searchMoviePanel.getProdCountriesFilters(),
				this.searchMoviePanel.getLangagesFilters(),
				this.searchMoviePanel.getGenresFilters(),
				this.searchMoviePanel.getRealisatorsFilters(),
				this.searchMoviePanel.getActorsFilters()
		);
		this.searchMoviePanel.setFilmList(searchedFilms);
	}
	
	private void rentMovie() {
		Film selectedMovie = this.searchMoviePanel.getSelectedFilm();
		if (selectedMovie != null) {
			this.controller.rentMovie(selectedMovie);
		}
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();
		if (source == this.btSearchMovie) {
			this.launchSearch();
		} else if (source == this.btRentMovie) {
			this.rentMovie();
		}
	}
}
